package com.tjetc.controller;

import com.tjetc.constant.CommonConstant;
import com.tjetc.context.BaseContext;
import com.tjetc.exception.MessageException;
import com.tjetc.pojo.dto.AdminDTO;
import com.tjetc.pojo.dto.CheckInDTO;
import com.tjetc.service.*;
import com.tjetc.service.impl.*;
import com.tjetc.utils.ScannerUtils;
import com.tjetc.view.*;
import com.tjetc.view.impl.*;

/**
 * @Author: itda
 * @Date: 2024/5/9 14:50
 * @Description: 管理员控制层
 */
public class AdminController {

    private AdminService adminService = new AdminServiceImpl();

    private BookingService bookingService = new BookingServiceImpl();

    private RoomService roomService = new RoomServiceImpl();

    private OrderService orderService = new OrderServiceImpl();

    private CommentService commentService = new CommentServiceImpl();

    private PayService payService = new PayServiceImpl();

    private ExcelService excelService = new ExcelServiceImpl();

    private HotelService hotelService = new HotelServiceImpl();

    private HotelInfoPage hotelInfoPage = new HotelInfoPageImpl();

    private BookingPage bookingPage = new BookingPageImpl();

    private BookingCRUDPage bookingCRUDPage = new BookingCRUDPageImpl();

    private RoomStatusPage roomStatusPage = new RoomStatusPageImpl();

    private OrderPage orderPage = new OrderPageImpl();

    private OrderCRUDPage orderCRUDPage = new OrderCRUDPageImpl();

    private CommentPage commentPage = new CommentPageImpl();

    private CommonPage commonPage = new CommonPageImpl();

    private ExcelPage excelPage = new ExcelPageImpl();

    private PayPage payPage = new PayPageImpl();

    private PayCRUDPage payCRUDPage = new PayCRUDPageImpl();

    /**
     * 管理员登录
     */
    public void login(AdminDTO adminDTO) {
        try {
            if (BaseContext.getCurrentId() != null) {
                throw new MessageException(CommonConstant.ALREADY_LOGGED);
            }
            adminService.login(adminDTO);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void info() {
        try {
            if (BaseContext.getCurrentId() == null) {
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            hotelInfoPage.show();
        } catch (MessageException e) {
            System.out.println(e.getMessage());
        }
    }

    public void reserve() {
        try {
            if (BaseContext.getCurrentId() == null) {
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            while (true) {
                switch (bookingPage.show()) {
                    case 1 -> bookingService.save(bookingCRUDPage.save());
                    case 2 -> bookingService.delete(bookingCRUDPage.delete());
                    case 3 -> bookingService.update(bookingCRUDPage.update());
                    case 4 -> bookingService.list();
                    case 5 -> {
                        return;
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void status() {
        try {
            if (BaseContext.getCurrentId() == null) {
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            while (true) {
                int option = roomStatusPage.show();
                if (option == 5) {
                    return;
                }
                roomService.getByStatus(option);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

    public void payment() {
        try {
            if (BaseContext.getCurrentId() == null) {
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            while (true) {
                switch (payPage.show()) {
                    case 1:
                        payService.save(payCRUDPage.save());
                        break;
                    case 2:
                        payService.delete(payCRUDPage.delete());
                        break;
                    case 3:
                        payService.update(payCRUDPage.update());
                        break;
                    case 4:
                        payService.list();
                        break;
                    case 5:
                        return;
                }
            }
        } catch (MessageException e) {
            System.out.println(e.getMessage());
        }
    }

    public void order() {
        try {
            if (BaseContext.getCurrentId() == null) {
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            while (true) {
                switch (orderPage.show()) {
                    case 1:
                        orderService.save(orderCRUDPage.save());
                        break;
                    case 2:
                        orderService.delete(orderCRUDPage.delete());
                        break;
                    case 3:
                        orderService.update(orderCRUDPage.update());
                        break;
                    case 4:
                        orderService.list();
                        break;
                    case 5:
                        return;
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void comment() {
        try {
            if (BaseContext.getCurrentId() == null) {
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            while (true) {
                switch (commentPage.show()) {
                    case 1:
                        commentService.list(commonPage.getByHotelId());
                        break;
                    case 2:
                        commentService.getRating(commonPage.getByHotelId());
                        break;
                    case 3:
                        return;
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void statistics() {
        try {
            if (BaseContext.getCurrentId() == null) {
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            switch (excelPage.show()) {
                case 1:
                    excelService.today();
                    break;
                case 2:
                    excelService.month();
                    break;
                case 3:
                    excelService.booking();
                    break;
                case 4:
                    break;
            }
        } catch (MessageException e) {
            System.out.println(e.getMessage());
        }
    }

    public void checkIn(CheckInDTO checkInDTO) {
        try {
            if (BaseContext.getCurrentId() == null) {
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            hotelService.checkIn(checkInDTO);
        } catch (MessageException e) {
            System.out.println(e.getMessage());
        }
    }

    public void checkOut(CheckInDTO checkOut) {
        try {
            if (BaseContext.getCurrentId() == null) {
                throw new MessageException(CommonConstant.NOT_LOGGED);
            }
            hotelService.checkOut(checkOut);
        } catch (MessageException e) {
            System.out.println(e.getMessage());
        }
    }
}
